package vn.edu.ptithcm.pim.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import vn.edu.ptithcm.pim.constant.Constants;
import vn.edu.ptithcm.pim.constant.UrlConstants;
import vn.edu.ptithcm.pim.dom.Company;
import vn.edu.ptithcm.pim.service.CompanyService;
import vn.edu.ptithcm.pim.service.UserService;
import vn.edu.ptithcm.pim.util.ParamUtil;

@SuppressWarnings("deprecation")
public class EditCompanyController extends SimpleFormController {
	private UserService userService;
	private CompanyService companyService;
	Logger log = Logger.getLogger(EditCompanyController.class);

	public EditCompanyController() {
		super();
		setSessionForm(true);
		setCommandName("company");
		setCommandClass(Company.class);
		setFormView(Constants.EDIT_COMPANY_URL);
		setSuccessView(UrlConstants.EDIT_COMPANY_URL);
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		return companyService.getAll().get(0);
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		Company company = (Company) command;
		companyService.saveOrUpdate(company);
		request.setAttribute("editOK", true);
		return showForm(request, response, errors);
	}

	@Override
	protected ModelAndView showForm(HttpServletRequest request,
			HttpServletResponse response, BindException errors)
			throws Exception {
		if (userService.isSignedIn(request)) {
			String id = ParamUtil.getString(request, "id", "");
			Map<String, Object> bean = new HashMap<String, Object>();
			String loginName = userService.getCurrentLogin(request);
			bean.put("isSigned", true);
			bean.put(Constants.LOGIN_NAME, loginName);
			bean.put(Constants.USER_NAME, userService.getUsername(loginName));
			bean.put("id", id);
			String returnToFullPageURL = ParamUtil.getString(request,
					Constants.RETURN_TO_FULL_PAGE_URL, "");
			if (returnToFullPageURL.isEmpty()) {
				returnToFullPageURL = request.getHeader("referer");
			}
			bean.put(Constants.RETURN_TO_FULL_PAGE_URL, returnToFullPageURL);
			request.setAttribute("bean", bean);
		} else {
			String id = ParamUtil.getString(request, "id", "");
			String redirectLink = UrlConstants.EDIT_COMPANY_URL
					+ UrlConstants.HTM + "?id=" + id;
			request.getSession().setAttribute("redirectLink", redirectLink);
			log.info("EditCompanyController || redirectLink : " + redirectLink);
			response.sendRedirect(UrlConstants.HOME_PAGE_URL + UrlConstants.HTM);
		}
		return super.showForm(request, response, errors);
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}
}
